package cn.outsourcing.supermarket.provider;

import org.apache.ibatis.jdbc.SQL;

/**
 * 购物车sql构造器
 *
 * @author gzkemays
 * @since 2021/10/22 22:56
 */
public class CartProvider {
  // FIXME: 返回结果错误，将 cart 的数据都进行了获取。
  public String getAddCartList(Integer userId) {
    return new SQL() {
      {
        SELECT("hc.*")
            .SELECT("(hc.number * hp.goods_weight) as weightCount")
            .FROM("hiolabs_cart hc")
            .LEFT_OUTER_JOIN("hiolabs_product hp on hp.id = hc.product_id")
            .WHERE("hc.user_id = " + userId)
            .AND()
            .WHERE("hc.is_delete = 0");
      }
    }.toString();
  }

  public String getOrderCartList() {
    return new SQL() {
      {
        SELECT("cart.*,(cart.goods_weight * cart.`number`) as weightCount")
            .FROM("hiolabs_cart cart")
            .WHERE("user_id = ${userId}")
            .AND()
            .WHERE("product_id in (" + getProductIdsByOrderId() + ")");
      }
    }.toString();
  }

  public String getProductIdsByOrderId() {
    return new SQL() {
      {
        SELECT("product_id").FROM("hiolabs_order_goods hog").WHERE("hog.order_id = ${orderId}");
      }
    }.toString();
  }

  public String getGoodsSpecificationMerge() {
    return new SQL() {
      {
        SELECT("hg.*", "(hgs.id) as specificationMainId", " hgs.*", "hp.goods_specification_ids")
            .FROM("hiolabs_product hp")
            .LEFT_OUTER_JOIN(
                "hiolabs_goods_specification hgs on hgs.id = hp.goods_specification_ids",
                "hiolabs_goods hg on hg.id = hp.goods_id")
            .WHERE("hp.id = ${productId}");
      }
    }.toString();
  }
}
